package com.oocl.crm.controller.impl;

import java.io.InputStream;
import java.io.OutputStream;
import java.util.Scanner;

import com.oocl.crm.controller.Controller;
import com.oocl.crm.parser.CmdParser;
import com.oocl.crm.parser.impl.CmdParserImpl;
import com.oocl.crm.pojo.Customer;
import com.oocl.crm.util.FormatPrinter;

public class ControllerImpl implements Controller{
	private CmdParser parser = new CmdParserImpl();
	
	public void commandProcess(InputStream in, OutputStream out) {
		Scanner scanner=new Scanner(in);
		String cmd = "";
		FormatPrinter.printHelpDoc("H", out);
		do{
			cmd=scanner.nextLine();
			if(!cmd.equalsIgnoreCase("")){
				try{
					String action=cmd.substring(0,1);
					switch (action) {
					case "L":
						Customer[] cs= parser.find(cmd);
						FormatPrinter.printCustomers(cs, out);
						break;
					case "A":
						parser.processAdd(cmd);
						System.out.println("添加成员成功！按L查看");
						break;
					case "D":
						parser.processDelete(cmd);
						System.out.println("删除成员成功，按L查看");
						break;
					case "U":
						parser.processUpdate(cmd);
						System.out.println("更新成员成功，按L查看");
						break;
					case "O":
						Customer[] customers = parser.processSort(cmd.substring(2));
						FormatPrinter.printCustomers(customers, out);
						break;
					case "H":
						FormatPrinter.printHelpDoc(cmd, out);
						break;
					case "Q":
						break;
					default:
						System.out.println("格式有误！");
						break;
					}
				}catch(Exception e){
					System.out.println("输入格式有误！！请重新输入！");
					FormatPrinter.printHelpDoc(cmd, out);
				}
			}else{
				System.out.println("请输入命令");
			}
        }while(!cmd.equals("Q")); 
		scanner.close();
				
	}

}
